package com.clj.fastble.bluetooth;

import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleIndicateCallback;
import com.clj.fastble.callback.BleMtuChangedCallback;
import com.clj.fastble.callback.BleNotifyCallback;
import com.clj.fastble.callback.BleReadCallback;
import com.clj.fastble.callback.BleRssiCallback;
import com.clj.fastble.callback.BleWriteCallback;
import com.clj.fastble.data.BleMsg;
import com.clj.fastble.data.BleWriteState;
import com.clj.fastble.exception.GattException;
import com.clj.fastble.exception.OtherException;
import com.clj.fastble.exception.TimeoutException;

import ohos.bluetooth.ble.BlePeripheralDevice;
import ohos.bluetooth.ble.GattCharacteristic;
import ohos.bluetooth.ble.GattDescriptor;
import ohos.bluetooth.ble.GattService;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.utils.PacMap;

import java.util.Optional;
import java.util.UUID;

import static com.clj.fastble.data.BleMsg.*;


/**
 * BleConnector
 */
public class BleConnector {
    private static final String UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR = "00002902-0000-1000-8000-00805f9b34fb";

    private BlePeripheralDevice mBluetoothGatt;
    private GattService mGattService;
    private GattCharacteristic mCharacteristic;
    private BleBluetooth mBleBluetooth;
    private EventHandler mHandler;

    BleConnector(BleBluetooth bleBluetooth) {
        this.mBleBluetooth = bleBluetooth;
        this.mBluetoothGatt = bleBluetooth.getBluetoothGatt();
        this.mHandler =
                new EventHandler(EventRunner.getMainEventRunner()) {
                    @Override
                    protected void processEvent(InnerEvent event) {
                        super.processEvent(event);
                        switch (event.eventId) {
                            case BleMsg.MSG_CHA_NOTIFY_START: {
                                BleNotifyCallback notifyCallback = (BleNotifyCallback) event.object;
                                if (notifyCallback != null) {
                                    notifyCallback.onNotifyFailure(new TimeoutException());
                                }
                                break;
                            }

                            case BleMsg.MSG_CHA_NOTIFY_RESULT: {
                                notifyMsgInit();

                                BleNotifyCallback notifyCallback = (BleNotifyCallback) event.object;
                                PacMap bundle = event.getPacMap();
                                int status = bundle.getIntValue(BleMsg.KEY_NOTIFY_BUNDLE_STATUS);
                                if (notifyCallback != null) {
                                    if (status == BlePeripheralDevice.OPERATION_SUCC) {
                                        notifyCallback.onNotifySuccess();
                                    } else {
                                        notifyCallback.onNotifyFailure(new GattException(status));
                                    }
                                }
                                break;
                            }

                            case BleMsg.MSG_CHA_NOTIFY_DATA_CHANGE: {
                                BleNotifyCallback notifyCallback = (BleNotifyCallback) event.object;
                                PacMap bundle = event.getPacMap();
                                byte[] value = bundle.getByteValueArray(BleMsg.KEY_NOTIFY_BUNDLE_VALUE);
                                if (notifyCallback != null) {
                                    notifyCallback.onCharacteristicChanged(value);
                                }
                                break;
                            }

                            case BleMsg.MSG_CHA_INDICATE_START: {
                                BleIndicateCallback indicateCallback = (BleIndicateCallback) event.object;
                                if (indicateCallback != null) {
                                    indicateCallback.onIndicateFailure(new TimeoutException());
                                }

                                break;
                            }

                            case BleMsg.MSG_CHA_INDICATE_RESULT: {
                                indicateMsgInit();

                                BleIndicateCallback indicateCallback = (BleIndicateCallback) event.object;
                                PacMap bundle = event.getPacMap();
                                int status = bundle.getIntValue(BleMsg.KEY_INDICATE_BUNDLE_STATUS);
                                if (indicateCallback != null) {
                                    if (status == BlePeripheralDevice.OPERATION_SUCC) {
                                        indicateCallback.onIndicateSuccess();
                                    } else {
                                        indicateCallback.onIndicateFailure(new GattException(status));
                                    }
                                }
                                break;
                            }

                            case BleMsg.MSG_CHA_INDICATE_DATA_CHANGE: {
                                BleIndicateCallback indicateCallback = (BleIndicateCallback) event.object;
                                PacMap bundle = event.getPacMap();
                                byte[] value = bundle.getByteValueArray(BleMsg.KEY_INDICATE_BUNDLE_VALUE);
                                if (indicateCallback != null) {
                                    indicateCallback.onCharacteristicChanged(value);
                                }
                                break;
                            }

                            case BleMsg.MSG_CHA_WRITE_START: {
                                BleWriteCallback writeCallback = (BleWriteCallback) event.object;
                                if (writeCallback != null) {
                                    writeCallback.onWriteFailure(new TimeoutException());
                                }
                                break;
                            }

                            case BleMsg.MSG_CHA_WRITE_RESULT: {
                                writeMsgInit();

                                BleWriteCallback writeCallback = (BleWriteCallback) event.object;
                                PacMap bundle = event.getPacMap();
                                int status = bundle.getIntValue(BleMsg.KEY_WRITE_BUNDLE_STATUS);
                                byte[] value = bundle.getByteValueArray(BleMsg.KEY_WRITE_BUNDLE_VALUE);
                                if (writeCallback != null) {
                                    if (status == BlePeripheralDevice.OPERATION_SUCC) {
                                        writeCallback.onWriteSuccess(
                                                BleWriteState.DATA_WRITE_SINGLE,
                                                BleWriteState.DATA_WRITE_SINGLE,
                                                value);
                                    } else {
                                        writeCallback.onWriteFailure(new GattException(status));
                                    }
                                }
                                break;
                            }

                            case BleMsg.MSG_CHA_READ_START: {
                                BleReadCallback readCallback = (BleReadCallback) event.object;
                                if (readCallback != null) {
                                    readCallback.onReadFailure(new TimeoutException());
                                }
                                break;
                            }

                            case BleMsg.MSG_CHA_READ_RESULT: {
                                readMsgInit();

                                BleReadCallback readCallback = (BleReadCallback) event.object;
                                PacMap bundle = event.getPacMap();
                                int status = bundle.getIntValue(BleMsg.KEY_READ_BUNDLE_STATUS);
                                byte[] value = bundle.getByteValueArray(BleMsg.KEY_READ_BUNDLE_VALUE);
                                if (readCallback != null) {
                                    if (status == BlePeripheralDevice.OPERATION_SUCC) {
                                        readCallback.onReadSuccess(value);
                                    } else {
                                        readCallback.onReadFailure(new GattException(status));
                                    }
                                }
                                break;
                            }

                            case BleMsg.MSG_READ_RSSI_START: {
                                BleRssiCallback rssiCallback = (BleRssiCallback) event.object;
                                if (rssiCallback != null) {
                                    rssiCallback.onRssiFailure(new TimeoutException());
                                }
                                break;
                            }

                            case BleMsg.MSG_READ_RSSI_RESULT: {
                                rssiMsgInit();

                                BleRssiCallback rssiCallback = (BleRssiCallback) event.object;
                                PacMap bundle = event.getPacMap();
                                int status = bundle.getIntValue(BleMsg.KEY_READ_RSSI_BUNDLE_STATUS);
                                int value = bundle.getIntValue(BleMsg.KEY_READ_RSSI_BUNDLE_VALUE);
                                if (rssiCallback != null) {
                                    if (status == BlePeripheralDevice.OPERATION_SUCC) {
                                        rssiCallback.onRssiSuccess(value);
                                    } else {
                                        rssiCallback.onRssiFailure(new GattException(status));
                                    }
                                }
                                break;
                            }

                            case BleMsg.MSG_SET_MTU_START: {
                                BleMtuChangedCallback mtuChangedCallback = (BleMtuChangedCallback) event.object;
                                if (mtuChangedCallback != null) {
                                    mtuChangedCallback.onSetMTUFailure(new TimeoutException());
                                }
                                break;
                            }

                            case BleMsg.MSG_SET_MTU_RESULT: {
                                mtuChangedMsgInit();

                                BleMtuChangedCallback mtuChangedCallback = (BleMtuChangedCallback) event.object;
                                PacMap bundle = event.getPacMap();
                                int status = bundle.getIntValue(BleMsg.KEY_SET_MTU_BUNDLE_STATUS);
                                int value = bundle.getIntValue(BleMsg.KEY_SET_MTU_BUNDLE_VALUE);
                                if (mtuChangedCallback != null) {
                                    if (status == BlePeripheralDevice.OPERATION_SUCC) {
                                        mtuChangedCallback.onMtuChanged(value);
                                    } else {
                                        mtuChangedCallback.onSetMTUFailure(new GattException(status));
                                    }
                                }
                                break;
                            }
                        }
                    }
                };
    }

    private BleConnector withUUID(UUID serviceUUID, UUID characteristicUUID) {
        if (serviceUUID != null && mBluetoothGatt != null) {
            Optional<GattService> gattServiceOptional = mBluetoothGatt.getService(serviceUUID);
            mGattService = gattServiceOptional.get();
        }
        if (mGattService != null && characteristicUUID != null) {
            mCharacteristic = mGattService.getCharacteristic(characteristicUUID).get();
        }
        return this;
    }

    /**
     * withUUIDString
     *
     * @param serviceUUID        serviceUUID
     * @param characteristicUUID characteristicUUID
     * @return BleConnector
     */
    public BleConnector withUUIDString(String serviceUUID, String characteristicUUID) {
        return withUUID(formUUID(serviceUUID), formUUID(characteristicUUID));
    }

    private UUID formUUID(String uuid) {
        return uuid == null ? null : UUID.fromString(uuid);
    }

    /* ------------------------------- main operation ----------------------------------- */

    /**
     * notify
     *
     * @param bleNotifyCallback            bleNotifyCallback
     * @param uuid_notify                  uuid_notify
     * @param userCharacteristicDescriptor userCharacteristicDescriptor
     */
    public void enableCharacteristicNotify(
            BleNotifyCallback bleNotifyCallback, String uuid_notify, boolean userCharacteristicDescriptor) {
        if (mCharacteristic != null && (mCharacteristic.getProperties() | PROPERTY_NOTIFY) > 0) {
            handleCharacteristicNotifyCallback(bleNotifyCallback, uuid_notify);
            setCharacteristicNotification(
                    mBluetoothGatt, mCharacteristic, userCharacteristicDescriptor, true, bleNotifyCallback);
        } else {
            if (bleNotifyCallback != null) {
                bleNotifyCallback.onNotifyFailure(new OtherException("this characteristic not support notify!"));
            }
        }
    }

    /**
     * stop notify
     *
     * @param useCharacteristicDescriptor useCharacteristicDescriptor
     * @return boolean
     */
    public boolean disableCharacteristicNotify(boolean useCharacteristicDescriptor) {
        if (mCharacteristic != null && (mCharacteristic.getProperties() | PROPERTY_NOTIFY) > 0) {
            return setCharacteristicNotification(
                    mBluetoothGatt, mCharacteristic, useCharacteristicDescriptor, false, null);
        } else {
            return false;
        }
    }

    /**
     * notify setting
     *
     * @param gatt                        gatt
     * @param characteristic              characteristic
     * @param useCharacteristicDescriptor useCharacteristicDescriptor
     * @param enable                      enable
     * @param bleNotifyCallback           bleNotifyCallback
     * @return boolean
     */
    private boolean setCharacteristicNotification(
            BlePeripheralDevice gatt,
            GattCharacteristic characteristic,
            boolean useCharacteristicDescriptor,
            boolean enable,
            BleNotifyCallback bleNotifyCallback) {
        if (gatt == null || characteristic == null) {
            notifyMsgInit();
            if (bleNotifyCallback != null) {
                bleNotifyCallback.onNotifyFailure(new OtherException("gatt or characteristic equal null"));
            }
            return false;
        }

        boolean success1 = gatt.setNotifyCharacteristic(characteristic, enable);
        if (!success1) {
            notifyMsgInit();
            if (bleNotifyCallback != null) {
                bleNotifyCallback.onNotifyFailure(new OtherException("gatt setCharacteristicNotification fail"));
            }
            return false;
        }

        GattDescriptor descriptor;
        if (useCharacteristicDescriptor) {
            descriptor = characteristic.getDescriptor(characteristic.getUuid()).get();
        } else {
            descriptor = characteristic.getDescriptor(formUUID(UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR)).get();
        }
        if (descriptor == null) {
            notifyMsgInit();
            if (bleNotifyCallback != null) {
                bleNotifyCallback.onNotifyFailure(new OtherException("descriptor equals null"));
            }
            return false;
        } else {
            descriptor.setValue(enable ? ENABLE_NOTIFICATION_VALUE : DISABLE_NOTIFICATION_VALUE);
            boolean success2 = gatt.writeDescriptor(descriptor);
            if (!success2) {
                notifyMsgInit();
                if (bleNotifyCallback != null) {
                    bleNotifyCallback.onNotifyFailure(new OtherException("gatt writeDescriptor fail"));
                }
            }
            return success2;
        }
    }

    /**
     * indicate
     *
     * @param bleIndicateCallback         bleIndicateCallback
     * @param uuid_indicate               uuid_indicate
     * @param useCharacteristicDescriptor useCharacteristicDescriptor
     */
    public void enableCharacteristicIndicate(
            BleIndicateCallback bleIndicateCallback, String uuid_indicate, boolean useCharacteristicDescriptor) {
        if (mCharacteristic != null && (mCharacteristic.getProperties() | PROPERTY_NOTIFY) > 0) {
            handleCharacteristicIndicateCallback(bleIndicateCallback, uuid_indicate);
            setCharacteristicIndication(
                    mBluetoothGatt, mCharacteristic, useCharacteristicDescriptor, true, bleIndicateCallback);
        } else {
            if (bleIndicateCallback != null) {
                bleIndicateCallback.onIndicateFailure(new OtherException("this characteristic not support indicate!"));
            }
        }
    }

    /**
     * stop indicate
     *
     * @param userCharacteristicDescriptor userCharacteristicDescriptor
     * @return boolean
     */
    public boolean disableCharacteristicIndicate(boolean userCharacteristicDescriptor) {
        if (mCharacteristic != null && (mCharacteristic.getProperties() | PROPERTY_NOTIFY) > 0) {
            return setCharacteristicIndication(
                    mBluetoothGatt, mCharacteristic, userCharacteristicDescriptor, false, null);
        } else {
            return false;
        }
    }

    /**
     * indicate setting
     *
     * @param gatt                        gatt
     * @param characteristic              characteristic
     * @param useCharacteristicDescriptor useCharacteristicDescriptor
     * @param enable                      enable
     * @param bleIndicateCallback         bleIndicateCallback
     * @return boolean
     */
    private boolean setCharacteristicIndication(
            BlePeripheralDevice gatt,
            GattCharacteristic characteristic,
            boolean useCharacteristicDescriptor,
            boolean enable,
            BleIndicateCallback bleIndicateCallback) {
        if (gatt == null || characteristic == null) {
            indicateMsgInit();
            if (bleIndicateCallback != null) {
                bleIndicateCallback.onIndicateFailure(new OtherException("gatt or characteristic equal null"));
            }
            return false;
        }

        boolean success1 = gatt.setNotifyCharacteristic(characteristic, enable);
        if (!success1) {
            indicateMsgInit();
            if (bleIndicateCallback != null) {
                bleIndicateCallback.onIndicateFailure(new OtherException("gatt setCharacteristicNotification fail"));
            }
            return false;
        }

        GattDescriptor descriptor;
        if (useCharacteristicDescriptor) {
            descriptor = characteristic.getDescriptor(characteristic.getUuid()).get();
        } else {
            descriptor = characteristic.getDescriptor(formUUID(UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR)).get();
        }
        if (descriptor == null) {
            indicateMsgInit();
            if (bleIndicateCallback != null) {
                bleIndicateCallback.onIndicateFailure(new OtherException("descriptor equals null"));
            }
            return false;
        } else {
            descriptor.setValue(enable ? ENABLE_INDICATION_VALUE : DISABLE_NOTIFICATION_VALUE);
            boolean success2 = gatt.writeDescriptor(descriptor);
            if (!success2) {
                indicateMsgInit();
                if (bleIndicateCallback != null) {
                    bleIndicateCallback.onIndicateFailure(new OtherException("gatt writeDescriptor fail"));
                }
            }
            return success2;
        }
    }

    /**
     * write
     *
     * @param data             data
     * @param bleWriteCallback bleWriteCallback
     * @param uuid_write       uuid_write
     */
    public void writeCharacteristic(byte[] data, BleWriteCallback bleWriteCallback, String uuid_write) {
        if (data == null || data.length <= 0) {
            if (bleWriteCallback != null) {
                bleWriteCallback.onWriteFailure(new OtherException("the data to be written is empty"));
            }
            return;
        }

        if (mCharacteristic == null
                || (mCharacteristic.getProperties()
                & (GattCharacteristic.PROPERTY_WRITE | GattCharacteristic.PROPERTY_WRITE_NO_RESPONSE))
                == 0) {
            if (bleWriteCallback != null) {
                bleWriteCallback.onWriteFailure(new OtherException("this characteristic not support write!"));
            }
            return;
        }

        if (mCharacteristic.setValue(data)) {
            handleCharacteristicWriteCallback(bleWriteCallback, uuid_write);
            if (!mBluetoothGatt.writeCharacteristic(mCharacteristic)) {
                writeMsgInit();
                if (bleWriteCallback != null) {
                    bleWriteCallback.onWriteFailure(new OtherException("gatt writeCharacteristic fail"));
                }
            }
        } else {
            if (bleWriteCallback != null) {
                bleWriteCallback.onWriteFailure(
                        new OtherException("Updates the locally stored value of this characteristic fail"));
            }
        }
    }

    /**
     * read
     *
     * @param bleReadCallback bleReadCallback
     * @param uuid_read       uuid_read
     */
    public void readCharacteristic(BleReadCallback bleReadCallback, String uuid_read) {
        if (mCharacteristic != null && (mCharacteristic.getProperties() & GattCharacteristic.PROPERTY_READ) > 0) {
            handleCharacteristicReadCallback(bleReadCallback, uuid_read);
            if (!mBluetoothGatt.readCharacteristic(mCharacteristic)) {
                readMsgInit();
                if (bleReadCallback != null) {
                    bleReadCallback.onReadFailure(new OtherException("gatt readCharacteristic fail"));
                }
            }
        } else {
            if (bleReadCallback != null) {
                bleReadCallback.onReadFailure(new OtherException("this characteristic not support read!"));
            }
        }
    }

    /**
     * rssi
     *
     * @param bleRssiCallback bleRssiCallback
     */
    public void readRemoteRssi(BleRssiCallback bleRssiCallback) {
        handleRSSIReadCallback(bleRssiCallback);
        if (!mBluetoothGatt.readRemoteRssiValue()) {
            rssiMsgInit();
            if (bleRssiCallback != null) {
                bleRssiCallback.onRssiFailure(new OtherException("gatt readRemoteRssi fail"));
            }
        }
    }

    /**
     * set mtu
     *
     * @param requiredMtu           requiredMtu
     * @param bleMtuChangedCallback bleMtuChangedCallback
     */
    public void setMtu(int requiredMtu, BleMtuChangedCallback bleMtuChangedCallback) {
        handleSetMtuCallback(bleMtuChangedCallback);
        if (!mBluetoothGatt.requestBleMtuSize(requiredMtu)) {
            mtuChangedMsgInit();
            if (bleMtuChangedCallback != null) {
                bleMtuChangedCallback.onSetMTUFailure(new OtherException("gatt requestMtu fail"));
            }
        }
    }

    /**
     * requestConnectionPriority
     *
     * @param connectionPriority Request a specific connection priority. Must be one of
     *                           {@link BlePeripheralDevice#CONNECTION_PRIORITY_NORMAL},
     *                           {@link BlePeripheralDevice#CONNECTION_PRIORITY_HIGH}
     *                           or {@link BlePeripheralDevice#CONNECTION_PRIORITY_LOW}.
     * @return boolean
     * @throws IllegalArgumentException If the parameters are outside of their
     *                                  specified range.
     */
    public boolean requestConnectionPriority(int connectionPriority) {
        return mBluetoothGatt.requestBleConnectionPriority(connectionPriority);
    }

    /* *************************************** Handle call back ***************************************** */

    /**
     * notify
     *
     * @param bleNotifyCallback bleNotifyCallback
     * @param uuid_notify       uuid_notify
     */
    private void handleCharacteristicNotifyCallback(BleNotifyCallback bleNotifyCallback, String uuid_notify) {
        if (bleNotifyCallback != null) {
            notifyMsgInit();
            bleNotifyCallback.setKey(uuid_notify);
            bleNotifyCallback.setHandler(mHandler);
            mBleBluetooth.addNotifyCallback(uuid_notify, bleNotifyCallback);
            mHandler.sendEvent(
                    InnerEvent.get(BleMsg.MSG_CHA_NOTIFY_START, bleNotifyCallback),
                    BleManager.getInstance().getOperateTimeout());
        }
    }

    /**
     * indicate
     *
     * @param bleIndicateCallback bleIndicateCallback
     * @param uuid_indicate       uuid_indicate
     */
    private void handleCharacteristicIndicateCallback(BleIndicateCallback bleIndicateCallback, String uuid_indicate) {
        if (bleIndicateCallback != null) {
            indicateMsgInit();
            bleIndicateCallback.setKey(uuid_indicate);
            bleIndicateCallback.setHandler(mHandler);
            mBleBluetooth.addIndicateCallback(uuid_indicate, bleIndicateCallback);
            mHandler.sendEvent(
                    InnerEvent.get(BleMsg.MSG_CHA_INDICATE_START, bleIndicateCallback),
                    BleManager.getInstance().getOperateTimeout());
        }
    }

    /**
     * write
     *
     * @param bleWriteCallback bleWriteCallback
     * @param uuid_write       uuid_write
     */
    private void handleCharacteristicWriteCallback(BleWriteCallback bleWriteCallback, String uuid_write) {
        if (bleWriteCallback != null) {
            writeMsgInit();
            bleWriteCallback.setKey(uuid_write);
            bleWriteCallback.setHandler(mHandler);
            mBleBluetooth.addWriteCallback(uuid_write, bleWriteCallback);
            mHandler.sendEvent(
                    InnerEvent.get(BleMsg.MSG_CHA_WRITE_START, bleWriteCallback),
                    BleManager.getInstance().getOperateTimeout());
        }
    }

    /**
     * read
     *
     * @param bleReadCallback bleReadCallback
     * @param uuid_read       uuid_read
     */
    private void handleCharacteristicReadCallback(BleReadCallback bleReadCallback, String uuid_read) {
        if (bleReadCallback != null) {
            readMsgInit();
            bleReadCallback.setKey(uuid_read);
            bleReadCallback.setHandler(mHandler);
            mBleBluetooth.addReadCallback(uuid_read, bleReadCallback);
            mHandler.sendEvent(
                    InnerEvent.get(BleMsg.MSG_CHA_READ_START, bleReadCallback),
                    BleManager.getInstance().getOperateTimeout());
        }
    }

    /**
     * rssi
     *
     * @param bleRssiCallback bleRssiCallback
     */
    private void handleRSSIReadCallback(BleRssiCallback bleRssiCallback) {
        if (bleRssiCallback != null) {
            rssiMsgInit();
            bleRssiCallback.setHandler(mHandler);
            mBleBluetooth.addRssiCallback(bleRssiCallback);
            mHandler.sendEvent(
                    InnerEvent.get(BleMsg.MSG_READ_RSSI_START, bleRssiCallback),
                    BleManager.getInstance().getOperateTimeout());
        }
    }

    /**
     * set mtu
     *
     * @param bleMtuChangedCallback bleMtuChangedCallback
     */
    private void handleSetMtuCallback(BleMtuChangedCallback bleMtuChangedCallback) {
        if (bleMtuChangedCallback != null) {
            mtuChangedMsgInit();
            bleMtuChangedCallback.setHandler(mHandler);
            mBleBluetooth.addMtuChangedCallback(bleMtuChangedCallback);
            mHandler.sendEvent(
                    InnerEvent.get(BleMsg.MSG_SET_MTU_START, bleMtuChangedCallback),
                    BleManager.getInstance().getOperateTimeout());
        }
    }

    /**
     * notifyMsgInit
     */
    public void notifyMsgInit() {
        mHandler.removeEvent(BleMsg.MSG_CHA_NOTIFY_START);
    }

    /**
     * indicateMsgInit
     */
    public void indicateMsgInit() {
        mHandler.removeEvent(BleMsg.MSG_CHA_INDICATE_START);
    }

    /**
     * writeMsgInit
     */
    public void writeMsgInit() {
        mHandler.removeEvent(BleMsg.MSG_CHA_WRITE_START);
    }

    /**
     * readMsgInit
     */
    public void readMsgInit() {
        mHandler.removeEvent(BleMsg.MSG_CHA_READ_START);
    }

    /**
     * rssiMsgInit
     */
    public void rssiMsgInit() {
        mHandler.removeEvent(BleMsg.MSG_READ_RSSI_START);
    }

    /**
     * mtuChangedMsgInit
     */
    public void mtuChangedMsgInit() {
        mHandler.removeEvent(BleMsg.MSG_SET_MTU_START);
    }
}
